//
//  HadHandleWorkOrderViewController.m
//  NewPro
//
//  Created by RYX on 2018/11/23.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "HadHandleWorkOrderViewController.h"
#import "WorkOrderCell.h"
#import "WorkOrderDetailViewController.h"
#import "WorkOrderStore.h"

@interface HadHandleWorkOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) NSArray *listArr;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) ViewHeader *header;

@end

@implementation HadHandleWorkOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
}


- (void)setUI{
    
    [self setNavBarWithTitle:@"已处理工单"];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.equalTo(self.view);
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
}
- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 9.5;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}

- (void)refresh{
    
    WorkOrderStore *store = [[WorkOrderStore alloc] init];
    
    NSString *customId = [UserDefaultsManager sharedInstance].user_id;
    
    MJWeakSelf
    [store getWorkOrderProcessedListWithCustomId:customId Success:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [WorkOrderCell getHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkOrderCell *cell = [WorkOrderCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.listArr[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    WorkOrderDetailViewController *VC = [[WorkOrderDetailViewController alloc] init];
//    [self.navigationController pushViewController:VC animated:YES];
    
}

@end
